package com.collection.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.collection.dao.ContentStrategyDao;
import com.collection.entity.ContentStrategy;
import com.collection.service.ContentStrategyService;

@Service
public class ContentStrategyServiceImpl implements ContentStrategyService {

	@Autowired
	private ContentStrategyDao contentStrategyDao;
	
	/*
	 * (non-Javadoc)
	 * @see com.collection.service.ContentStrategyService#createContentStrategy(com.collection.entity.ContentStrategy)
	 */
	@Override
	public void createContentStrategy(ContentStrategy contentStrategy) {
		if(contentStrategy != null) {
			contentStrategyDao.save(contentStrategy);
			contentStrategyDao.createCollectionTable(contentStrategy);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.ContentStrategyService#deleteContentStrategy(java.lang.Integer)
	 */
	@Override
	public void deleteContentStrategy(Integer id) {
		if(id != null) {
			ContentStrategy contentStrategy = getContentStrategyById(id);
			if(contentStrategy != null) {
				deleteContentStrategy(contentStrategy);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.ContentStrategyService#deleteContentStrategy(com.collection.entity.ContentStrategy)
	 */
	@Override
	public void deleteContentStrategy(ContentStrategy contentStrategy) {
		if(contentStrategy != null) {
			contentStrategyDao.delete(contentStrategy);
			contentStrategyDao.deleteCollectionTable(contentStrategy);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.ContentStrategyService#getContentStrategyById(java.lang.Integer)
	 */
	@Override
	public ContentStrategy getContentStrategyById(Integer id) {
		if(id != null) {
			return contentStrategyDao.get(ContentStrategy.class, id);
		}
		return null;
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.collection.service.ContentStrategyService#getContentStrategyByTaskId(java.lang.Integer)
	 */
	@Override
	public ContentStrategy getContentStrategyByTaskId(Integer taskId) {
		if(taskId != null) {
			List<ContentStrategy> contentStrategies = contentStrategyDao.queryByHQL("from ContentStrategy cs where cs.collectionTask.id=?", new Object[]{taskId});
			if(contentStrategies != null && contentStrategies.size()>0) {
				return contentStrategies.get(0);
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.collection.service.ContentStrategyService#modifyContentStrategy(com.collection.entity.ContentStrategy)
	 */
	@Override
	public void modifyContentStrategy(ContentStrategy contentStrategy) {
		if(contentStrategy != null) {
			contentStrategyDao.update(contentStrategy);
		}
	}

}
